package com.fang.config.listener;

import com.fang.config.event.MsgEvent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.Clock;
import java.util.concurrent.TimeUnit;

/**
 * 自定义监听器
 * @author Fang Ruichuan
 * @date 2022-09-13 17:11
 */
@Slf4j
@Component
public class MsgListener {

    @SneakyThrows
    @Async
    @EventListener(MsgEvent.class)
    public void sendMsg(MsgEvent event) {
        String orderId = event.getOrderId();
        long start = Clock.systemUTC().millis();
        log.info("开始发送短信");
        log.info("开始发送邮件");
        TimeUnit.MILLISECONDS.sleep(4000);
        long end = Clock.systemUTC().millis();
        log.info("{}: 发送短信、邮件耗时：({})毫秒", orderId, (end - start));
    }

}
